﻿namespace RialternativeSample.Tests.Server.Northwind
{
    using System.Linq;
    using Microsoft.Practices.Unity;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using RialternativeSample.Server.Infrastructure;
    using RialternativeSample.Server.NorthwindService;
    using RialternativeSample.Tests.Server.Northwind.Infrastructure;

    [TestClass]
    public class NorthwindTest
    {
        private NorthwindService northwindService;

        [TestInitialize]
        public void Initialize()
        {
            UnityProvider.Instance.UnityContainer.RegisterType<NorthwindService>(new InjectionProperty("Repository", typeof(NorthwindMockRepository)));
            this.northwindService = UnityProvider.Instance.UnityContainer.Resolve<NorthwindService>();
        }

        [TestMethod]
        public void CanGetCustomersTest()
        {
            Customers customer = this.northwindService.GetQuery<Customers>().FirstOrDefault();
            Assert.IsNotNull(customer);
            Assert.AreEqual("Paris", customer.City);
        }

        [TestMethod]
        public void CanUpdateCustomersTest()
        {
            Customers customer = this.northwindService.GetQuery<Customers>().FirstOrDefault();
            customer.City = "London";
            this.northwindService.Update(customer, null);

            Customers updatedCustomer = this.northwindService.GetQuery<Customers>().FirstOrDefault();
            Assert.IsNotNull(customer);
            Assert.AreEqual("London", customer.City);
        }

        [TestMethod]
        public void CanAddCustomersTest()
        {
            const string customerId = "CUSNY03";
            this.northwindService.Insert(new Customers { CustomerID = customerId });
            Customers insertedCustomer = this.northwindService.GetQuery<Customers>()
                                                              .Where(customer => customer.CustomerID == customerId)
                                                              .FirstOrDefault();
            Assert.IsNotNull(insertedCustomer);
            Assert.AreEqual(insertedCustomer.CustomerID, customerId);
        }

        [TestMethod]
        public void CanDeleteCustomersTest()
        {
            Customers customerToDelete = this.northwindService.GetQuery<Customers>()
                                                              .Where(customer => customer.CustomerID == "CUSPA01")
                                                              .FirstOrDefault();
            this.northwindService.Delete(customerToDelete);
            Assert.IsNull(this.northwindService.GetQuery<Customers>()
                                               .Where(customer => customer.CustomerID == "CUSPA01")
                                               .FirstOrDefault());
        }
    }
}
